/*************************************************************************
	> File Name: ts.cc
	> Author: likang
	> Mail:  
	> Created Time: Wed 15 Apr 2015 08:33:17 PM CST
 ************************************************************************/

#include<iostream>
#include<memory>
#include<string>

using namespace std;



class A {
public:
	A(int age, string name):age_(age), name_(name){}
	typedef shared_ptr <A> pointer;
	int age_;
	string name_;
};

A* a;

void test() {
	//如果均已a作为参数的话，shared_ptr是分开单独计数的，test结束后，会进行两次free
	a = new A(2,"kk");
	A::pointer p1(a);
	//cout << "count:" << p1.use_count() << endl;
	//A::pointer p2(a);
	//cout << "count:" << p2.use_count() << endl;
	cout << "success" << endl;
}

void foo(A::pointer &p) {
	cout << "foo count" << p.use_count() << endl;
}

void bar(A::pointer p) {
	cout << "bar count" << p.use_count() << endl;
}
	

int main(void) {
	test();
	cout << "age:" << a->age_ << " str:"<< a->name_ <<endl;
	A::pointer root_;
	A::pointer node(new A(3,"ss"));
	root_ = node;
	cout << "age:" << root_->age_ << " str:" << root_->name_ << " count" << root_.use_count() << " " << node.use_count() << endl;
	foo(root_);
	bar(root_);
	cout << "count " << root_.use_count() << endl;
	A::pointer &p = root_;
	cout << "count " << root_.use_count() << endl;
	return 0;

}
